import { Injectable, OnInit } from '@angular/core';
import { User } from './user';
import { Users } from './mock-users'
import { TimeService } from './time.service';
@Injectable({
    providedIn: 'root'
})
export class UserService implements OnInit {

    listOfData: User[] = Users;
    //编辑时临时用户数组变量
    temporaryUser: User[] = Users;
    //获得全部用户
    getUsers(): User[] {
        return this.listOfData;
    }
    //删除一个用户
    deleteUser(id: string): void {
        this.listOfData = this.listOfData.filter(d => d.id !== id);
    }
    //添加一个用户
    addUser(userName: string, number: string, password: string, radioValue: string) {
        this.listOfData = this.listOfData.concat(Array.of({ id: (Number(this.listOfData[this.listOfData.length - 1].id) + 1).toString(), name: userName, number: number, status: radioValue, createTime: TimeService.getNowDate(), password: password }));
        console.log(this.listOfData.length)
    }
    //修改一个用户
    editUser(id: string, userName: string, number: string, password: string, radioValue: string) {
        // this.temporaryUser.splice(Number(id) - 1, 1, { id: id, name: userName, number: number, status: radioValue, createTime: new Date(), password: password });
        // this.listOfData = this.temporaryUser;
        //this.listOfData = this.listOfData.filter(d => d.id !== id);
        //this.listOfData = Array.of({ id: id, name: userName, number: number, status: radioValue, createTime: new Date(), password: password }).concat(this.listOfData)
        this.listOfData = this.listOfData.map(function (user) {
            if (user.id == id) {
                return { id: id, name: userName, number: number, status: radioValue, createTime: TimeService.getNowDate(), password: password };
            } else {
                return user;
            }
        })
    }
    constructor(public timeService: TimeService) { }
    ngOnInit(): void {
    }
}